home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / bit / src / ulib / readstr.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  1KB  |  47 lines

  1. /***********************************************************************
  2.  * $Id: readstr.c,v 0.80 1994/02/24 09:48:11 zhao Exp $
  3.  *
  4.  *.  Copyright(c) 1993,1994 by T.C. Zhao
  5.  *   All rights reserved.
  6.  *.
  7.  *
  8.  *  Read a token from stream file until EOF or newline or SPACE,
  9.  *  but  keep at most max characters in buffer s and append '\0'
  10.  *  at the end and delete the newline. Returns no. of characters
  11.  *  read. Returns EOF only if no characters are read.
  12.  ***********************************************************************/
  13. #if !defined(lint) && defined(F_ID)
  14. char *id_getstr = "$Id: readstr.c,v 0.80 1994/02/24 09:48:11 zhao Exp $";
  15. #endif
  16.  
  17. #include <stdio.h>
  18. #include "ulib.h"
  19.  
  20. #define IS_SEPERATOR(c) ((c) == '\n' || (c) == ' ')
  21.  
  22. int
  23. readstring(FILE * file, char s[], register int n)
  24. {
  25.     register int i, c;
  26.     i = 0;
  27.     n--;
  28.     while ((c = getc(file)) != EOF && !IS_SEPERATOR(c))
  29.     if (i < n)
  30.         s[i++] = c;
  31.     s[i] = '\0';
  32.     return (c == EOF && i == 0 ? EOF : i);
  33. }
  34.  
  35. int
  36. readline(FILE * file, char s[], register int n)
  37. {
  38.     register int i, c;
  39.     i = 0;
  40.     n--;
  41.     while ((c = getc(file)) != EOF && c != '\n')
  42.     if (i < n)
  43.         s[i++] = c;
  44.     s[i] = '\0';
  45.     return (c == EOF && i == 0 ? EOF : i);
  46. }
  47.